def test_good_databasic(array, location, mappings):
    data = spatial.DataBasic(
        array=array,
        location=location,
        mappings=mappings,
    )
    assert data.validate()
Beispiel #2
0
def translate_data_array(steno3d_data, location, view, _lookup_dict=None):
    """Translate Steno3D array data into Data, Mapping, and Arrays"""
    arr = files.Array(steno3d_data.array)
    data = spatial.DataBasic(
        name=steno3d_data.title or '',
        description=steno3d_data.description or '',
        array=arr,
        location=location,
    )
    if steno3d_data.colormap is not None:
        gradient = files.Array(steno3d_data.colormap)
        mapping = spatial.MappingContinuous(
            gradient=gradient,
            data_controls=[
                np.nanmin(steno3d_data.array),
                np.nanmax(steno3d_data.array),
            ],
            gradient_controls=[0., 1.],
            visibility=[True, True, True],
        )
        data.mappings = [mapping]
        view.contents += [mapping, gradient]
    view.contents += [arr, data]
    if _lookup_dict is not None and hasattr(steno3d_data, '_upload_data'):
        _lookup_dict.update({steno3d_data._upload_data['uid']: data})
    return data
Beispiel #3
0
def translate_data_discrete(steno3d_data, location, view, _lookup_dict=None):
    """Translate Steno3D discrete data into Data, Mapping, and Array"""
    arr = files.Array(steno3d_data.array)
    if steno3d_data.range_visibility is not None:
        visibility = [bool(rv) for rv in steno3d_data.range_visibility]
    else:
        visibility = [True] * (len(steno3d_data.end_values) + 1)
    if steno3d_data.end_inclusive is not None:
        end_inclusive = [bool(ei) for ei in steno3d_data.end_inclusive]
    else:
        end_inclusive = [True] * len(steno3d_data.end_values)
    mapping = spatial.MappingDiscrete(
        end_points=steno3d_data.end_values,
        visibility=visibility,
        end_inclusive=end_inclusive,
        values=steno3d_data.colormap
        or [''] * (len(steno3d_data.end_values) + 1),
    )
    data = spatial.DataBasic(name=steno3d_data.title or '',
                             description=steno3d_data.description or '',
                             array=arr,
                             location=location,
                             mappings=[mapping])
    view.contents += [arr, mapping, data]
    if _lookup_dict is not None and hasattr(steno3d_data, '_upload_data'):
        _lookup_dict.update({steno3d_data._upload_data['uid']: data})
    return data
def test_sanitize_colormaps():
    data = spatial.DataBasic()
    data = utils.sanitize_data_colormaps(data)
    assert len(data.mappings) == 0
    data.mappings = [spatial.MappingContinuous()]
    data = utils.sanitize_data_colormaps(data)
    assert len(data.mappings) == 1
    data.mappings = [
        spatial.MappingDiscrete(values=[1., 2, 3]),
        spatial.MappingContinuous(),
        spatial.MappingDiscrete(values=['r', 'g', 'b']),
    ]
    data = utils.sanitize_data_colormaps(data)
    assert len(data.mappings) == 3
    assert data.mappings[0].values == [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
    assert data.mappings[1].values == [1., 2, 3]
    assert isinstance(data.mappings[2], spatial.MappingContinuous)
    data = spatial.DataCategory(
        categories=
        'https://example.com/api/v1/view/org1/proj1/view1/mappings/category/abc123',
    )
    data = utils.sanitize_data_colormaps(data)
    assert len(data.mappings) == 1
    assert data.mappings[0] == data.categories
    data.mappings = [spatial.MappingCategory(values=[1., 2, 3])]
    data.categories = spatial.MappingCategory(values=[1., 2, 3])
    data = utils.sanitize_data_colormaps(data)
    assert len(data.mappings) == 2
    assert isinstance(data.mappings[0].values[0], tuple)
    assert data.mappings[1].values == [1., 2, 3]
def test_bad_databasic(prop, bad_val):
    data = spatial.DataBasic(
        array='https://example.com/api/files/array/abc123',
        location='N',
    )
    with pytest.raises(properties.ValidationError):
        setattr(data, prop, bad_val)
        data.validate()
def test_extra_slide_validation():
    element_list = [
        'https://example.com/api/v1/view/org1/proj1/view1/elements/pointset/abc123',
        'https://example.com/api/v1/view/org1/proj1/view1/elements/surface/def456',
    ]
    slide = scene.Slide(scene=scene.Scene())
    slide.scene.plots[0].views = [
        scene.PointSet(element=spatial.ElementPointSet())
    ]
    with pytest.raises(properties.ValidationError):
        utils.extra_slide_validation(slide, element_list)
    slide.scene.plots[0].views = [
        scene.PointSet(
            element=
            'https://example.com/api/v1/view/org1/proj1/view1/elements/pointset/abc123',
        )
    ]
    with pytest.raises(properties.ValidationError):
        utils.extra_slide_validation(slide, element_list)
    slide.scene.plots[0].views = [
        scene.PointSet(
            element=
            'https://example.com/api/v1/view/org1/proj1/view1/elements/pointset/abc123',
        ),
        scene.Surface(
            element=
            'https://example.com/api/v1/view/org1/proj1/view1/elements/surface/def456',
        )
    ]
    utils.extra_slide_validation(slide, element_list)
    slide.scene.plots[0].views[
        0].color.mapping = 'https://example.com/api/v1/view/org1/proj1/view1/mappings/continuous/def456'
    slide.scene.plots[0].views[0].color.data = spatial.DataBasic()
    with pytest.raises(properties.ValidationError):
        utils.extra_slide_validation(slide, element_list)
    slide.scene.plots[0].views[0].color.mapping = properties.undefined
    slide.scene.plots[0].views[0].color.data = properties.undefined
    slide.scene.plots[0].views[1].textures = [{
        'data':
        spatial.TextureProjection(),
    }]
    with pytest.raises(properties.ValidationError):
        utils.extra_slide_validation(slide, element_list)
Beispiel #7
0
def test_upload(mock_regex, mock_upload_image, mock_upload_array, mock_put,
                mock_patch, mock_post, verbose, workers, parallel, session):
    mock_resp = mock.MagicMock()
    mock_resp.json.return_value = {
        'links': {
            'self': 'https://example.com/api/self',
            'location': 'https://example.com/api/location',
            'thumbnail': 'https://example.com/api/self/thumbnail'
        },
    }
    mock_resp.ok = True
    mock_post.return_value = mock_resp
    mock_patch.return_value = mock_resp
    mock_put.return_value = mock_resp
    mock_file_resp = mock.MagicMock()
    mock_file_resp.json.return_value = {}
    mock_file_resp.ok = True
    mock_upload_array.return_value = mock_file_resp
    mock_upload_image.return_value = mock_file_resp
    mock_regex.return_value = re.compile(r'^https://example\.com/api/')

    mapping_uploaded = spatial.MappingDiscrete(
        values=[(255, 0, 0), (0, 255, 0), (0, 0, 255)],
        end_points=[10., 20.],
        end_inclusive=[True, True],
        visibility=[True, True, True],
    )
    mapping_uploaded._links = {
        'self': 'https://example.com/api/mapping_uploaded'
    }
    array_data = files.Array([0., 10, 20])
    img = io.BytesIO()
    s = [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]]
    w = png.Writer(4, 4, greyscale=True, bitdepth=16)
    w.write(img, s)

    view = manifests.View(
        name='Test View',
        elements=[
            spatial.ElementPointSet(
                vertices=files.Array([[0., 0, 0], [1, 1, 1], [2, 2, 2]]),
                data=[
                    spatial.DataBasic(
                        name='Dataset 1',
                        location='n',
                        array=array_data,
                        uid='local_id',
                    ),
                    spatial.DataBasic(
                        name='Dataset 2',
                        description='Same array as dataset 1',
                        location='n',
                        array=array_data,
                        mappings=[
                            spatial.MappingContinuous(
                                gradient='https://example.com/api/my_colormap',
                                data_controls=[0., 10., 20., 30.],
                            ),
                            mapping_uploaded,
                        ]),
                    spatial.TextureProjection(
                        origin=[0., 0, 0],
                        axis_u=[1., 0, 0],
                        axis_v=[0., 1, 0],
                        image=img,
                    ),
                ],
                defaults={
                    'color': {
                        'value': '#FF0000'
                    },
                    'opacity': {
                        'value': 1
                    },
                    'size': {
                        'value': 10
                    },
                    'shape': 'square'
                },
            ),
            'https://example.com/api/my_element',
        ])
    try:
        dirname, _ = os.path.split(os.path.abspath(__file__))
        png_file = os.path.sep.join(dirname.split(os.path.sep) + ['temp.png'])
        s = ['110010010011', '101011010100', '110010110101', '100010010011']
        s = [[int(v) for v in val] for val in s]
        f = open(png_file, 'wb')
        w = png.Writer(len(s[0]), len(s), greyscale=True, bitdepth=16)
        w.write(f, s)
        f.close()

        session.upload(
            view,
            verbose=verbose,
            thumbnail=png_file,
            parallel=parallel,
            workers=workers,
        )
    finally:
        os.remove(png_file)

    assert mock_post.call_count == 9
    assert mock_patch.call_count == 1
    assert mock_put.call_count == 1
    assert mock_upload_array.call_count == 2
    assert mock_upload_image.call_count == 2

    mock_post.assert_has_calls(
        [
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/files/array',
                json={
                    'shape': [3, 3],
                    'dtype': 'Float64Array',
                    'content_type': 'application/octet-stream',
                    'content_length': 31,
                    'content_encoding': 'gzip'
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/files/array',
                json={
                    'shape': [3],
                    'dtype': 'Float64Array',
                    'content_type': 'application/octet-stream',
                    'content_length':
                    24,  # this file is 29 bytes when compressed
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/files/image',
                json={
                    'content_type': 'image/png',
                    'content_length': img.seek(0, 2),
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/mappings/continuous',
                json={
                    'gradient': 'https://example.com/api/my_colormap',
                    'data_controls': [0., 10., 20., 30.],
                    'gradient_controls': [0., 0., 1., 1.],
                    'visibility': [False, True, True, True, False],
                    'interpolate': False,
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/data/basic',
                json={
                    'name': 'Dataset 1',
                    'location': 'nodes',
                    'array': 'https://example.com/api/self',
                    'mappings': [],
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/data/basic',
                json={
                    'name':
                    'Dataset 2',
                    'description':
                    'Same array as dataset 1',
                    'location':
                    'nodes',
                    'array':
                    'https://example.com/api/self',
                    'mappings': [
                        'https://example.com/api/self',
                        'https://example.com/api/mapping_uploaded',
                    ],
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/textures/projection',
                json={
                    'origin': [0., 0, 0],
                    'axis_u': [1., 0, 0],
                    'axis_v': [0., 1, 0],
                    'image': 'https://example.com/api/self',
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/elements/pointset',
                json={
                    'vertices':
                    'https://example.com/api/self',
                    'data': [
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                    ],
                    'defaults': {
                        'visible': True,
                        'color': {
                            'value': '#FF0000'
                        },
                        'opacity': {
                            'value': 1
                        },
                        'size': {
                            'value': 10
                        },
                        'shape': 'square'
                    },
                },
            ),
            mock.call(
                'https://example.com/api/v1/project/myorg/myproj/views',
                json={
                    'name':
                    'Test View',
                    'elements': [
                        'https://example.com/api/self',
                        'https://example.com/api/my_element',
                    ],
                    'contents': [
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                        'https://example.com/api/my_colormap',
                        'https://example.com/api/self',
                        'https://example.com/api/mapping_uploaded',
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                        'https://example.com/api/self',
                        'https://example.com/api/my_element',
                    ],
                },
            ),
        ],
        any_order=True)
    mock_patch.assert_called_with(
        'https://example.com/api/mapping_uploaded',
        json={
            'values': ['#FF0000', '#00FF00', '#0000FF'],
            'end_points': [10., 20.],
            'end_inclusive': [True, True],
            'visibility': [True, True, True],
        },
    )
    mock_put.assert_called_with(
        'https://example.com/api/self/thumbnail',
        json={
            'content_type': 'image/png',
            'content_length': 88,
        },
    )