def section2():
    return Section.create(image=Image(channels=np.empty((2, 4, 4)),
                                      resolution_um=3.4),
                          physical_transform=PhysicalTransformer(x=5,
                                                                 y=10,
                                                                 z=2,
                                                                 rx=2,
                                                                 ry=15,
                                                                 rz=-3))
def test_register_section_returns_error_message_if_no_atlas_loaded(section1):
    repo = Mock(BaseRepo)
    repo.get_atlas.return_value = None
    repo.get_sections.return_value = [
        Section.create(
            image=Image(channels=np.empty((2, 4, 4)), resolution_um=3.4))
    ]
    command = UpdateSectionCommand(_repo=repo)
    result = command(UpdateSectionRequest(section_id=section1.id, steps=[]))
    assert "no atlas" in result.value.lower()
def test_center_atlas_command_centers_section_when_atlas_is_loaded():
    section = Section.create(
        image=Image(channels=np.empty((2, 4, 4)), resolution_um=3.4),
        physical_transform=PhysicalTransformer(x=0, y=0, z=0),
    )
    repo = Mock(BaseRepo)
    repo.get_section.return_value = section
    repo.get_atlas.return_value = Atlas(volume=np.empty((3, 3, 3)),
                                        annotation_volume=np.empty((3, 3, 3)),
                                        resolution_um=10)
    move_section = UpdateSectionCommand(_repo=repo)
    request = UpdateSectionRequest(section_id=section.id, steps=[Center()])
    result = move_section(request)
    data = result.unwrap()
    assert data.superior == 15
    assert data.anterior == 15
    assert data.right == 15
def test_move_section_to_rotation_rotates_it_and_returns_new_position(
        value, axis):
    physical_transform = PhysicalTransformer(rx=5, ry=10, rz=2)
    section = Section.create(image=Image(channels=np.empty((2, 4, 4)),
                                         resolution_um=3.4),
                             physical_transform=physical_transform)
    repo = Mock(BaseRepo)
    repo.get_section.return_value = section

    repo.get_atlas.return_value = Atlas(volume=np.empty((5, 5, 5)),
                                        resolution_um=10)
    move_section = UpdateSectionCommand(_repo=repo)
    request = UpdateSectionRequest(section_id=section.id,
                                   steps=[SetRotation(axis=axis, value=value)])
    result = move_section(request)
    data = result.unwrap()
    assert data.rot_longitudinal == value if axis is Axis.Longitudinal else 5
    assert data.rot_anteroposterior == value if axis is Axis.Anteroposterior else 10
    assert data.rot_horizontal == value if axis is Axis.Horizontal else 2
def test_relative_move_section_to_position_translates_it_and_returns_new_position(
        value, direction):
    physical_transform = PhysicalTransformer(x=5, y=10, z=2)
    section = Section.create(image=Image(channels=np.empty((2, 4, 4)),
                                         resolution_um=3.4),
                             physical_transform=physical_transform)
    repo = Mock(BaseRepo)
    repo.get_section.return_value = section
    repo.get_atlas.return_value = Atlas(volume=np.empty((3, 3, 3)),
                                        annotation_volume=np.empty((3, 3, 3)),
                                        resolution_um=10)
    move_section = UpdateSectionCommand(_repo=repo)
    request = UpdateSectionRequest(
        section_id=section.id,
        steps=[Translate(direction=direction, value=value)])
    result = move_section(request)
    data = result.unwrap()
    if direction is Direction.Superior:
        assert data.superior == 5 + value
    elif direction is Direction.Inferior:
        assert data.superior == 5 - value
    else:
        assert data.superior == 5

    if direction is Direction.Anterior:
        assert data.anterior == 10 + value
    elif direction is Direction.Posterior:
        assert data.anterior == 10 - value
    else:
        assert data.anterior == 10

    if direction is Direction.Right:
        assert data.right == 2 + value
    elif direction is Direction.Left:
        assert data.right == 2 - value
    else:
        assert data.right == 2