Example #1
0
File: shapes.py Project: cplab/ceed
def assert_add_three_groups(
        shape_factory: CeedPaintCanvasBehavior = None,
        app: CeedTestApp = None, show_in_gui=False):
    shape, shape2, shape3 = assert_add_three_shapes(
        shape_factory, app, show_in_gui)
    assert not shape_factory.groups
    assert not shape_factory.shape_group_names

    group = shape_factory.add_group()
    group2 = shape_factory.add_group()
    group3 = shape_factory.add_group()

    group.add_shape(shape.shape)
    group.add_shape(shape2.shape)
    group2.add_shape(shape2.shape)
    group2.add_shape(shape3.shape)
    group3.add_shape(shape.shape)
    group3.add_shape(shape2.shape)
    group3.add_shape(shape3.shape)

    assert shape_factory.groups == [group, group2, group3]
    assert len(shape_factory.groups) == 3
    assert len(shape_factory.shape_group_names) == 3
    for g in (group, group2, group3):
        assert shape_factory.shape_group_names[g.name] is g

    assert group.shapes == [shape.shape, shape2.shape]
    assert group2.shapes == [shape2.shape, shape3.shape]
    assert group3.shapes == [shape.shape, shape2.shape, shape3.shape]

    return (group, group2, group3), (shape, shape2, shape3)
Example #2
0
File: shapes.py Project: cplab/ceed
def assert_add_three_shapes(
        shape_factory: CeedPaintCanvasBehavior = None,
        app: CeedTestApp = None, show_in_gui=False):
    assert not shape_factory.shapes
    assert not shape_factory.shape_names

    shape = EllipseShapeP1(
        app=app, painter=shape_factory, show_in_gui=show_in_gui)
    shape2 = PolygonShapeP1(
        app=app, painter=shape_factory, show_in_gui=show_in_gui)
    shape3 = CircleShapeP1(
        app=app, painter=shape_factory, show_in_gui=show_in_gui)

    if not show_in_gui:
        shape.make_shape()
        shape2.make_shape()
        shape3.make_shape()

        assert shape_factory.add_shape(shape.shape)
        assert shape_factory.add_shape(shape2.shape)
        assert shape_factory.add_shape(shape3.shape)

    assert shape_factory.shapes == [shape.shape, shape2.shape, shape3.shape]
    assert len(shape_factory.shapes) == 3
    assert len(shape_factory.shape_names) == 3
    for s in (shape, shape2, shape3):
        assert shape_factory.shape_names[s.name] is s.shape
    return shape, shape2, shape3
Example #3
0
def test_set_factory_state(shape_factory: CeedPaintCanvasBehavior):
    (group, group2, group3), (shape, shape2, shape3) = \
        assert_add_three_groups(shape_factory)

    name_map = {}
    shape_factory.test_changes_count = 0
    shape_factory.set_state(shape_factory.get_state(), name_map)
    assert shape_factory.test_changes_count

    assert shape_factory.shapes[:3] == [
        shape.shape, shape2.shape, shape3.shape
    ]
    assert len(shape_factory.shapes) == 6
    assert len(set(shape_factory.shapes)) == 6
    assert len(shape_factory.shape_names) == 6

    assert shape_factory.groups[:3] == [group, group2, group3]
    assert len(shape_factory.groups) == 6
    assert len(set(shape_factory.groups)) == 6
    assert len(shape_factory.shape_group_names) == 6

    shape4, shape5, shape6 = shape_factory.shapes[3:]
    group4, group5, group6 = shape_factory.groups[3:]

    assert group4.shapes == [shape4, shape5]
    assert group5.shapes == [shape5, shape6]
    assert group6.shapes == [shape4, shape5, shape6]

    assert_shapes_same(shape.shape, shape4)
    assert_shapes_same(shape2.shape, shape5)
    assert_shapes_same(shape3.shape, shape6)
Example #4
0
def test_move_shape_upwards(shape_factory: CeedPaintCanvasBehavior):
    shape, shape2, shape3 = assert_add_three_shapes(shape_factory)

    shape_factory.test_changes_count = 0
    shape_factory.move_shape_upwards(shape.shape)
    assert shape_factory.test_changes_count

    assert shape_factory.shapes == [shape2.shape, shape.shape, shape3.shape]
    assert len(shape_factory.shapes) == 3
    assert len(shape_factory.shape_names) == 3
    for s in (shape, shape2, shape3):
        assert shape_factory.shape_names[s.name] is s.shape
Example #5
0
def test_shape_add_remove(shape_factory: CeedPaintCanvasBehavior, shape_cls):
    assert not shape_factory.shapes
    assert not shape_factory.shape_names
    shape = shape_cls(app=None, painter=shape_factory, show_in_gui=False)
    shape.make_shape()

    # add shape
    shape_factory.test_changes_count = 0
    assert shape_factory.add_shape(shape.shape)
    assert shape.shape in shape_factory.shapes
    assert shape.name in shape_factory.shape_names
    assert shape.shape is shape_factory.shape_names[shape.name]
    assert shape_factory.test_changes_count

    # remove shape
    shape_factory.test_changes_remove_shape_count = 0
    shape_factory.test_changes_count = 0
    assert shape_factory.remove_shape(shape.shape)
    assert shape.shape not in shape_factory.shapes
    assert shape.name not in shape_factory.shape_names
    assert shape_factory.test_changes_remove_shape_count
    assert shape_factory.test_changes_count

    # remove same shape again
    shape_factory.test_changes_remove_shape_count = 0
    shape_factory.test_changes_count = 0
    assert not shape_factory.remove_shape(shape.shape)
    assert not shape_factory.test_changes_remove_shape_count
    assert not shape_factory.test_changes_count
Example #6
0
def test_remove_from_group(shape_factory: CeedPaintCanvasBehavior):
    shape, shape2, shape3 = assert_add_three_shapes(shape_factory)

    group = shape_factory.add_group()
    add_prop_watch(group, 'on_changed', 'test_changes_count')
    assert group.add_shape(shape.shape)
    assert group.add_shape(shape2.shape)
    assert group.shapes == [shape.shape, shape2.shape]

    # remove first shape
    group.test_changes_count = 0
    group.remove_shape(shape.shape)
    assert group.test_changes_count
    assert group.shapes == [shape2.shape]

    # removing again does nothing
    group.remove_shape(shape.shape)
    assert group.shapes == [shape2.shape]

    # remove last
    group.test_changes_count = 0
    group.remove_shape(shape2.shape)
    assert group.test_changes_count
    assert not group.shapes

    # removing again does nothing
    group.remove_shape(shape2.shape)
    assert not group.shapes
Example #7
0
def shape_factory() -> CeedPaintCanvasBehavior:
    shape_factory = CeedPaintCanvasBehavior()
    add_prop_watch(shape_factory, 'on_remove_shape',
                   'test_changes_remove_shape_count')
    add_prop_watch(shape_factory, 'on_remove_group',
                   'test_changes_remove_group_count')
    add_prop_watch(shape_factory, 'on_changed', 'test_changes_count')

    yield shape_factory
Example #8
0
def test_area(shape_factory: CeedPaintCanvasBehavior):
    shape = CircleShapeP1(app=None, painter=shape_factory, show_in_gui=False)
    shape.make_shape()

    assert shape_factory.add_shape(shape.shape)
    assert math.isclose(shape.shape.area, shape.area)

    shape.shape.set_area(shape.shape.area / 4)
    assert math.isclose(shape.shape.radius, shape.radius / 2)
Example #9
0
def test_shape_name(shape_factory: CeedPaintCanvasBehavior):
    assert not shape_factory.shapes
    assert not shape_factory.shape_names
    shape = EllipseShapeP1(app=None, painter=shape_factory, show_in_gui=False)
    shape.make_shape()

    # add first shape
    shape_factory.test_changes_count = 0
    assert shape_factory.add_shape(shape.shape)
    assert len(shape_factory.shapes) == 1
    assert len(shape_factory.shape_names) == 1
    assert shape.shape in shape_factory.shapes
    assert shape.name in shape_factory.shape_names
    assert shape.shape is shape_factory.shape_names[shape.name]
    assert shape_factory.test_changes_count

    shape2 = EllipseShapeP1(app=None, painter=shape_factory, show_in_gui=False)
    shape2.make_shape()

    # add second shape
    shape_factory.test_changes_count = 0
    assert shape_factory.add_shape(shape2.shape)
    assert len(shape_factory.shapes) == 2
    assert len(shape_factory.shape_names) == 2
    assert shape2.shape in shape_factory.shapes
    assert shape_factory.test_changes_count

    assert shape2.name != shape2.shape.name
    assert shape.shape.name != shape2.shape.name
    assert shape2.shape.name in shape_factory.shape_names
    assert shape2.shape is shape_factory.shape_names[shape2.shape.name]

    # try making shape2 the same name as shape 1
    shape_factory.test_changes_count = 0
    shape2.shape.name = shape.shape.name
    assert len(shape_factory.shapes) == 2
    assert len(shape_factory.shape_names) == 2
    assert shape2.shape in shape_factory.shapes
    assert shape_factory.test_changes_count

    assert shape.shape.name != shape2.shape.name
    assert shape2.shape.name in shape_factory.shape_names
    assert shape2.shape is shape_factory.shape_names[shape2.shape.name]
Example #10
0
File: main.py Project: cplab/ceed
    def __init__(self, **kwargs):
        self.view_controller = ViewSideViewControllerBase()
        self.ceed_data = CeedDataWriterBase()
        self.data_serializer = DataSerializerBase()
        self.function_factory = FunctionFactoryBase()
        register_all_functions(self.function_factory)
        self.shape_factory = CeedPaintCanvasBehavior()
        self.stage_factory = StageFactoryBase(
            function_factory=self.function_factory,
            shape_factory=self.shape_factory)

        super(CeedViewApp, self).__init__(**kwargs)
Example #11
0
    def load_experiment(self, experiment):
        self._block = block = self._nix_file.blocks[
            CeedDataWriterBase.get_experiment_block_name(experiment)]
        section = self._nix_file.sections['experiment{}_metadata'.format(
            experiment)]
        self.loaded_experiment = experiment

        self.experiment_stage_name = section['stage']
        self.experiment_notes = section['notes'] if 'notes' in section else ''
        self.experiment_start_time = float(
            section['save_time']) if 'save_time' in section else 0
        config = section.sections['app_config']

        config_data = {}
        for prop in config.props:
            config_data[prop.name] = yaml_loads(read_nix_prop(prop))

        view = self.view_controller = ViewControllerBase()
        ser = self.data_serializer = DataSerializerBase()
        func = self.function_factory = FunctionFactoryBase()
        register_all_functions(func)
        shape = self.shape_factory = CeedPaintCanvasBehavior()
        stage = self.stage_factory = StageFactoryBase(function_factory=func,
                                                      shape_factory=shape)

        for name, obj in {
                'view': view,
                'serializer': ser,
                'function': func
        }.items():
            if name in config_data['app_settings']:
                apply_config(obj, config_data['app_settings'][name])
        self.populate_config(config_data, shape, func, stage)

        self.experiment_cam_image = self.read_image_from_block(self._block)

        data = self.shapes_intensity = {}
        for item in block.data_arrays:
            if not item.name.startswith('shape_'):
                continue
            data[item.name[6:]] = item

        self.led_state = block.data_arrays['led_state']

        if ('ceed_mcs_alignment' in self._nix_file.blocks
                and 'experiment_{}'.format(experiment)
                in self._nix_file.blocks['ceed_mcs_alignment'].data_arrays):
            self.electrode_intensity_alignment = self._nix_file.blocks[
                'ceed_mcs_alignment'].data_arrays['experiment_{}'.format(
                    experiment)]
        else:
            self.electrode_intensity_alignment = None
Example #12
0
def remove_shapes_upon_deletion(stage_factory: StageFactoryBase,
                                shape_factory: CeedPaintCanvasBehavior,
                                process_shape_callback):
    """Once called, whenever a shape or group of shapes is deleted in the
    ``shape_factory``, it'll also remove the shape or group from all stages
    that reference it.

    :param stage_factory: The :class:`StageFactoryBase` that lists all the
        stages.
    :param shape_factory: The :class:`~ceed.shape.CeedPaintCanvasBehavior` that
        lists all the shapes.
    :param process_shape_callback: For each stage in ``stage_factory`` that
        contains the shape, ``process_shape_callback`` will be called with 2
        parameters: the :class:`CeedStage` and :class:`StageShape` instances.
        The callback may e.g. then hide the shape in the GUI or whatever else
        it needs.
    """
    shape_factory.fbind('on_remove_shape',
                        stage_factory.find_shape_in_all_stages,
                        process_shape_callback=process_shape_callback)
    shape_factory.fbind('on_remove_group',
                        stage_factory.find_shape_in_all_stages,
                        process_shape_callback=process_shape_callback)
Example #13
0
def test_group_name(shape_factory: CeedPaintCanvasBehavior):
    assert not shape_factory.groups
    assert not shape_factory.shape_group_names

    # first group
    shape_factory.test_changes_count = 0
    group = shape_factory.add_group()

    assert isinstance(group, CeedShapeGroup)
    assert len(shape_factory.groups) == 1
    assert len(shape_factory.shape_group_names) == 1
    assert group in shape_factory.groups
    assert group.name in shape_factory.shape_group_names
    assert group is shape_factory.shape_group_names[group.name]
    assert shape_factory.test_changes_count

    # add second group
    shape_factory.test_changes_count = 0
    group2 = shape_factory.add_group()

    assert len(shape_factory.groups) == 2
    assert len(shape_factory.shape_group_names) == 2
    assert group2 in shape_factory.groups
    assert shape_factory.test_changes_count

    assert group.name != group2.name
    assert group2.name in shape_factory.shape_group_names
    assert group2 is shape_factory.shape_group_names[group2.name]

    # try making a duplicate name
    shape_factory.test_changes_count = 0
    group2.name = group.name
    assert len(shape_factory.groups) == 2
    assert len(shape_factory.shape_group_names) == 2
    assert group2 in shape_factory.groups
    assert shape_factory.test_changes_count

    assert group.name != group2.name
    assert group2.name in shape_factory.shape_group_names
    assert group2 is shape_factory.shape_group_names[group2.name]

    # try setting name
    shape_factory.test_changes_count = 0
    group2.name = 'something random'
    assert len(shape_factory.groups) == 2
    assert len(shape_factory.shape_group_names) == 2
    assert group2 in shape_factory.groups
    assert shape_factory.test_changes_count

    assert group2.name == 'something random'
    assert group2.name in shape_factory.shape_group_names
    assert group2 is shape_factory.shape_group_names['something random']
Example #14
0
def test_add_selection_to_group(shape_factory: CeedPaintCanvasBehavior):
    shape, shape2, shape3 = assert_add_three_shapes(shape_factory)
    # select first and second shape
    shape_factory.select_shape(shape.shape)
    shape_factory.select_shape(shape2.shape)

    group = shape_factory.add_group()
    add_prop_watch(group, 'on_changed', 'test_changes_count')

    # add first shape manually
    assert group.add_shape(shape.shape)
    assert group.shapes == [shape.shape]

    # add all selected shapes
    group.test_changes_count = 0
    shape_factory.add_selected_shapes_to_group(group)
    assert group.test_changes_count
    assert group.shapes == [shape.shape, shape2.shape]
Example #15
0
    def load_application_data(self):
        self.app_logs = self.app_notes = ''
        if 'app_logs' in self._nix_file.sections:
            self.app_logs = self._nix_file.sections['app_logs']['log_data']
            self.app_notes = self._nix_file.sections['app_logs']['notes']

        config = self._nix_file.sections['app_config']

        config_data = {}
        for prop in config.props:
            config_data[prop.name] = yaml_loads(read_nix_prop(prop))

        self.ceed_version = config_data.get('ceed_version', '')

        view = ViewControllerBase()
        ser = DataSerializerBase()
        func = FunctionFactoryBase()
        register_all_functions(func)
        shape = CeedPaintCanvasBehavior()
        stage = StageFactoryBase(function_factory=func, shape_factory=shape)

        for name, obj in {
                'view': view,
                'serializer': ser,
                'function': func
        }.items():
            if name in config_data['app_settings']:
                apply_config(obj, config_data['app_settings'][name])
        self.populate_config(config_data, shape, func, stage)

        self.app_config = {
            'view_controller': view,
            'data_serializer': ser,
            'function_factory': func,
            'shape_factory': shape,
            'stage_factory': stage,
        }
Example #16
0
def test_add_to_group(shape_factory: CeedPaintCanvasBehavior):
    shape, shape2, shape3 = assert_add_three_shapes(shape_factory)

    group = shape_factory.add_group()
    add_prop_watch(group, 'on_changed', 'test_changes_count')
    group.test_changes_count = 0
    assert group.add_shape(shape.shape)
    assert group.test_changes_count
    assert group.shapes == [shape.shape]

    # can't add twice
    assert not group.add_shape(shape.shape)
    assert group.shapes == [shape.shape]

    # add second shape
    group.test_changes_count = 0
    assert group.add_shape(shape2.shape)
    assert group.test_changes_count
    assert group.shapes == [shape.shape, shape2.shape]

    # can't add twice
    assert not group.add_shape(shape.shape)
    assert not group.add_shape(shape2.shape)
    assert group.shapes == [shape.shape, shape2.shape]
Example #17
0
def test_remove_from_all_groups(shape_factory: CeedPaintCanvasBehavior):
    (group, group2, group3), (shape, shape2, shape3) = \
        assert_add_three_groups(shape_factory)
    add_prop_watch(group, 'on_changed', 'test_changes_count')
    add_prop_watch(group2, 'on_changed', 'test_changes_count')
    add_prop_watch(group3, 'on_changed', 'test_changes_count')

    # remove first shape
    group.test_changes_count = 0
    group3.test_changes_count = 0
    shape_factory.remove_shape_from_groups(shape.shape)

    assert group.test_changes_count
    assert group3.test_changes_count
    assert group.shapes == [shape2.shape]
    assert group2.shapes == [shape2.shape, shape3.shape]
    assert group3.shapes == [shape2.shape, shape3.shape]

    # remove third shape
    group2.test_changes_count = 0
    group3.test_changes_count = 0
    shape_factory.remove_shape_from_groups(shape3.shape)

    assert group2.test_changes_count
    assert group3.test_changes_count
    assert group.shapes == [shape2.shape]
    assert group2.shapes == [shape2.shape]
    assert group3.shapes == [shape2.shape]

    # remove second shape
    group.test_changes_count = 0
    group2.test_changes_count = 0
    group3.test_changes_count = 0
    shape_factory.remove_shape_from_groups(shape2.shape)

    assert group.test_changes_count
    assert group2.test_changes_count
    assert group3.test_changes_count
    assert not group.shapes
    assert not group2.shapes
    assert not group3.shapes
Example #18
0
def test_center(shape_factory: CeedPaintCanvasBehavior):
    shape = CircleShapeP1(app=None, painter=shape_factory, show_in_gui=False)
    shape.make_shape()

    assert shape_factory.add_shape(shape.shape)
    assert shape.shape.centroid == tuple(shape.center)
Example #19
0
def test_add_remove_group(shape_factory: CeedPaintCanvasBehavior):
    assert not shape_factory.groups
    assert not shape_factory.shape_group_names

    # first group
    shape_factory.test_changes_count = 0
    group = shape_factory.add_group()

    assert isinstance(group, CeedShapeGroup)
    assert len(shape_factory.groups) == 1
    assert len(shape_factory.shape_group_names) == 1
    assert group in shape_factory.groups
    assert group.name in shape_factory.shape_group_names
    assert group is shape_factory.shape_group_names[group.name]
    assert shape_factory.test_changes_count

    # add second group
    shape_factory.test_changes_count = 0
    group2 = shape_factory.add_group()

    assert shape_factory.groups == [group, group2]
    assert shape_factory.shape_group_names == {
        group.name: group,
        group2.name: group2
    }
    assert shape_factory.test_changes_count

    # remove first group
    shape_factory.test_changes_count = 0
    shape_factory.test_changes_remove_group_count = 0
    assert shape_factory.remove_group(group)

    assert shape_factory.test_changes_count
    assert shape_factory.test_changes_remove_group_count
    assert shape_factory.groups == [group2]
    assert shape_factory.shape_group_names == {group2.name: group2}

    # add back first group
    shape_factory.test_changes_count = 0
    assert shape_factory.add_group(group) is group

    assert shape_factory.groups == [group2, group]
    assert shape_factory.shape_group_names == {
        group.name: group,
        group2.name: group2
    }
    assert shape_factory.test_changes_count

    # remove all groups
    shape_factory.test_changes_count = 0
    shape_factory.test_changes_remove_group_count = 0
    shape_factory.remove_all_groups()

    assert shape_factory.test_changes_count
    assert shape_factory.test_changes_remove_group_count
    assert not shape_factory.groups
    assert not shape_factory.shape_group_names
Example #20
0
def test_bounding_box(shape_factory: CeedPaintCanvasBehavior):
    shape = CircleShapeP1(app=None, painter=shape_factory, show_in_gui=False)
    shape.make_shape()

    assert shape_factory.add_shape(shape.shape)
    assert shape.shape.bounding_box == shape.bounding_box