Exemplo n.º 1
0
def test_format_manager():
    """ Test working of the format manager """
    
    formats = imageio.formats
    
    # Test basics of FormatManager
    assert isinstance(formats, FormatManager)
    assert len(formats) > 0
    assert 'FormatManager' in repr(formats)
    
    # Get docs
    smalldocs = str(formats)
    #fulldocs = formats.create_docs_for_all_formats()
    
    # Check each format ...
    for format in formats:
        #  That each format is indeed a Format
        assert isinstance(format, Format)
        # That they are mentioned
        assert format.name in smalldocs
        #assert format.name in fulldocs
    
    fname = get_remote_file('images/chelsea.png', test_dir)
    fname2 = fname[:-3] + 'noext'
    shutil.copy(fname, fname2)
    
    # Check getting
    F1 = formats['PNG']
    F2 = formats['.png']
    F3 = formats[fname2]  # will look in file itself
    assert F1 is F2
    assert F1 is F3
    # Check getting
    F1 = formats['DICOM']
    F2 = formats['.dcm']
    F3 = formats['dcm']  # If omitting dot, format is smart enough to try with
    assert F1 is F2
    assert F1 is F3
    # Fail
    raises(ValueError, formats.__getitem__, 678)  # must be str
    raises(IndexError, formats.__getitem__, '.nonexistentformat')
    
    # Adding a format
    myformat = Format('test', 'test description', 'testext1 testext2')
    formats.add_format(myformat)
    assert myformat in [f for f in formats]
    assert formats['testext1'] is myformat
    assert formats['testext2'] is myformat
    # Fail
    raises(ValueError, formats.add_format, 678)  # must be Format
    raises(ValueError, formats.add_format, myformat)  # cannot add twice
    
    # Test searchinhg for read / write format
    F = formats.search_read_format(Request(fname, 'ri'))
    assert F is formats['PNG']
    F = formats.search_save_format(Request(fname, 'wi'))
    assert F is formats['PNG']
Exemplo n.º 2
0
def test_format():
    """Test the working of the Format class"""

    filename1 = get_remote_file("images/chelsea.png", test_dir)
    filename2 = filename1 + ".out"

    # Test basic format creation
    F = Format("testname", "test description", "foo bar spam")
    assert F.name == "TESTNAME"
    assert F.description == "test description"
    assert F.name in repr(F)
    assert F.name in F.doc
    assert str(F) == F.doc
    assert set(F.extensions) == {".foo", ".bar", ".spam"}

    # Test setting extensions
    F1 = Format("test", "", "foo bar spam")
    F2 = Format("test", "", "foo, bar,spam")
    F3 = Format("test", "", ["foo", "bar", "spam"])
    F4 = Format("test", "", ".foo .bar .spam")
    for F in (F1, F2, F3, F4):
        assert set(F.extensions) == {".foo", ".bar", ".spam"}
    # Fail
    raises(ValueError, Format, "test", "", 3)  # not valid ext
    raises(ValueError, Format, "test", "", "", 3)  # not valid mode
    raises(ValueError, Format, "test", "", "", "x")  # not valid mode

    # Test subclassing
    F = MyFormat("test", "", modes="i")
    assert "TEST DOCS" in F.doc

    # Get and check reader and write classes
    R = F.get_reader(Request(filename1, "ri"))
    W = F.get_writer(Request(filename2, "wi"))
    assert isinstance(R, MyFormat.Reader)
    assert isinstance(W, MyFormat.Writer)
    assert R.format is F
    assert W.format is F
    assert R.request.filename == filename1
    assert W.request.filename == filename2
    # Fail
    raises(RuntimeError, F.get_reader, Request(filename1, "rI"))
    raises(RuntimeError, F.get_writer, Request(filename2, "wI"))

    # Use as context manager
    with R:
        pass
    with W:
        pass
    # Objects are now closed, cannot be used
    assert R.closed
    assert W.closed
    #
    raises(RuntimeError, R.__enter__)
    raises(RuntimeError, W.__enter__)
    #
    raises(RuntimeError, R.get_data, 0)
    raises(RuntimeError, W.append_data, np.zeros((10, 10)))

    # Test __del__
    R = F.get_reader(Request(filename1, "ri"))
    W = F.get_writer(Request(filename2, "wi"))
    ids = id(R), id(W)
    F._closed[:] = []
    del R
    del W
    gc.collect()  # Invoke __del__
    assert set(ids) == set(F._closed)
Exemplo n.º 3
0
def test_format_manager():
    """Test working of the format manager"""

    formats = imageio.formats

    # Test basics of FormatManager
    assert isinstance(formats, FormatManager)
    assert len(formats) > 0
    assert "FormatManager" in repr(formats)

    # Get docs
    smalldocs = str(formats)
    # fulldocs = formats.create_docs_for_all_formats()

    # Check each format ...
    for format in formats:
        #  That each format is indeed a Format
        assert isinstance(format, Format)
        # That they are mentioned
        assert format.name in smalldocs
        # assert format.name in fulldocs

    fname = get_remote_file("images/chelsea.png", test_dir)
    fname2 = fname[:-3] + "noext"
    shutil.copy(fname, fname2)

    # Check getting
    F1 = formats["PNG"]
    F2 = formats[".png"]
    F3 = formats[fname2]  # will look in file itself
    assert F1 is F2
    assert F1 is F3
    # Check getting
    F1 = formats["DICOM"]
    F2 = formats[".dcm"]
    F3 = formats["dcm"]  # If omitting dot, format is smart enough to try with
    assert F1 is F2
    assert F1 is F3
    # Fail
    raises(ValueError, formats.__getitem__, 678)  # must be str
    raises(IndexError, formats.__getitem__, ".nonexistentformat")

    # Adding a format
    myformat = Format("test", "test description", "testext1 testext2")
    formats.add_format(myformat)
    assert myformat in [f for f in formats]
    assert formats["testext1"] is myformat
    assert formats["testext2"] is myformat
    # Fail
    raises(ValueError, formats.add_format, 678)  # must be Format
    raises(ValueError, formats.add_format, myformat)  # cannot add twice

    # Adding a format with the same name
    myformat2 = Format("test", "other description", "foo bar")
    raises(ValueError, formats.add_format, myformat2)  # same name
    formats.add_format(myformat2, True)  # overwrite
    assert formats["test"] is not myformat
    assert formats["test"] is myformat2

    # Test show (we assume it shows correctly)
    formats.show()
Exemplo n.º 4
0
def test_format():
    """ Test the working of the Format class """

    filename1 = get_remote_file('images/chelsea.png', test_dir)
    filename2 = filename1 + '.out'

    # Test basic format creation
    F = Format('testname', 'test description', 'foo bar spam')
    assert F.name == 'TESTNAME'
    assert F.description == 'test description'
    assert F.name in repr(F)
    assert F.name in F.doc
    assert str(F) == F.doc
    assert set(F.extensions) == set(['.foo', '.bar', '.spam'])

    # Test setting extensions
    F1 = Format('test', '', 'foo bar spam')
    F2 = Format('test', '', 'foo, bar,spam')
    F3 = Format('test', '', ['foo', 'bar', 'spam'])
    F4 = Format('test', '', '.foo .bar .spam')
    for F in (F1, F2, F3, F4):
        assert set(F.extensions) == set(['.foo', '.bar', '.spam'])
    # Fail
    raises(ValueError, Format, 'test', '', 3)  # not valid ext
    raises(ValueError, Format, 'test', '', '', 3)  # not valid mode
    raises(ValueError, Format, 'test', '', '', 'x')  # not valid mode

    # Test subclassing
    F = MyFormat('test', '', modes='i')
    assert 'TEST DOCS' in F.doc

    # Get and check reader and write classes
    R = F.get_reader(Request(filename1, 'ri'))
    W = F.get_writer(Request(filename2, 'wi'))
    assert isinstance(R, MyFormat.Reader)
    assert isinstance(W, MyFormat.Writer)
    assert R.format is F
    assert W.format is F
    assert R.request.filename == filename1
    assert W.request.filename == filename2
    # Fail
    raises(RuntimeError, F.get_reader, Request(filename1, 'rI'))
    raises(RuntimeError, F.get_writer, Request(filename2, 'wI'))

    # Use as context manager
    with R:
        pass
    with W:
        pass
    # Objects are now closed, cannot be used
    assert R.closed
    assert W.closed
    #
    raises(RuntimeError, R.__enter__)
    raises(RuntimeError, W.__enter__)
    #
    raises(RuntimeError, R.get_data, 0)
    raises(RuntimeError, W.append_data, np.zeros((10, 10)))

    # Test __del__
    R = F.get_reader(Request(filename1, 'ri'))
    W = F.get_writer(Request(filename2, 'wi'))
    ids = id(R), id(W)
    F._closed[:] = []
    del R
    del W
    gc.collect()  # Invoke __del__
    assert set(ids) == set(F._closed)
Exemplo n.º 5
0
def test_format():
    """ Test the working of the Format class """
    
    filename1 = get_remote_file('images/chelsea.png', test_dir)
    filename2 = filename1 + '.out'
    
    # Test basic format creation
    F = Format('testname', 'test description', 'foo bar spam')
    assert F.name == 'TESTNAME'
    assert F.description == 'test description'
    assert F.name in repr(F)
    assert F.name in F.doc
    assert str(F) == F.doc
    assert set(F.extensions) == set(['foo', 'bar', 'spam'])
    
    # Test setting extensions
    F1 = Format('test', '', 'foo bar spam')
    F2 = Format('test', '', 'foo, bar,spam')
    F3 = Format('test', '', ['foo', 'bar', 'spam'])
    F4 = Format('test', '', '.foo .bar .spam')
    for F in (F1, F2, F3, F4):
        assert set(F.extensions) == set(['foo', 'bar', 'spam'])
    # Fail
    raises(ValueError, Format, 'test', '', 3)  # not valid ext
    raises(ValueError, Format, 'test', '', '', 3)  # not valid mode
    raises(ValueError, Format, 'test', '', '', 'x')  # not valid mode
    
    # Test subclassing
    F = MyFormat('test', '', modes='i')
    assert 'TEST DOCS' in F.doc
    
    # Get and check reader and write classes
    R = F.read(Request(filename1, 'ri'))
    W = F.save(Request(filename2, 'wi'))
    assert isinstance(R, MyFormat.Reader)
    assert isinstance(W, MyFormat.Writer)
    assert R.format is F
    assert W.format is F
    assert R.request.filename == filename1
    assert W.request.filename == filename2
    # Fail
    raises(RuntimeError, F.read, Request(filename1, 'rI'))
    raises(RuntimeError, F.save, Request(filename2, 'wI'))
    
    # Use as context manager
    with R:
        pass
    with W:
        pass
    # Objects are now closed, cannot be used
    assert R.closed
    assert W.closed
    #
    raises(RuntimeError, R.__enter__)
    raises(RuntimeError, W.__enter__)
    #
    raises(RuntimeError, R.get_data, 0)
    raises(RuntimeError, W.append_data, np.zeros((10, 10)))
    
    # Test __del__
    R = F.read(Request(filename1, 'ri'))
    W = F.save(Request(filename2, 'wi'))
    ids = id(R), id(W)
    F._closed[:] = []
    del R
    del W
    gc.collect()  # Invoke __del__
    assert set(ids) == set(F._closed)
    
    # Test using reader
    n = 3
    R = F.read(Request(filename1, 'ri'))
    assert len(R) == n
    ims = [im for im in R]
    assert len(ims) == n
    for i in range(3):
        assert ims[i][0, 0] == i
        assert ims[i].meta['index'] == i
    for i in range(3):
        assert R.get_meta_data(i)['index'] == i
    # Read next
    assert R.get_data(0)[0, 0] == 0
    assert R.get_next_data()[0, 0] == 1
    assert R.get_next_data()[0, 0] == 2
    # Fail
    R._failmode = True
    raises(ValueError, R.get_data, 0)
    raises(ValueError, R.get_meta_data, 0)
    
    # Test using writer
    im1 = np.zeros((10, 10))
    im2 = imageio.core.Image(im1, {'foo': 1})
    W = F.save(Request(filename2, 'wi'))
    W.append_data(im1)
    W.append_data(im2)
    W.append_data(im1, {'bar': 1})
    W.append_data(im2, {'bar': 1})
    # Test that no data is copies (but may be different views)
    assert len(W._written_data) == 4 
    for im in W._written_data:
        assert (im == im1).all()
    im1[2, 2] == 99
    for im in W._written_data:
        assert (im == im1).all()
    # Test meta
    assert W._written_meta[0] == {}
    assert W._written_meta[1] == {'foo': 1}
    assert W._written_meta[2] == {'bar': 1}
    assert W._written_meta[3] == {'foo': 1, 'bar': 1}
    #
    W.set_meta_data({'spam': 1})
    assert W._meta == {'spam': 1}
    # Fail
    raises(ValueError, W.append_data, 'not an array')
    raises(ValueError, W.append_data, im, 'not a dict')
    raises(ValueError, W.set_meta_data, 'not a dict')
Exemplo n.º 6
0
def test_format():
    """ Test the working of the Format class """
    
    filename1 = get_remote_file('images/chelsea.png', test_dir)
    filename2 = filename1 + '.out'
    
    # Test basic format creation
    F = Format('testname', 'test description', 'foo bar spam')
    assert F.name == 'TESTNAME'
    assert F.description == 'test description'
    assert F.name in repr(F)
    assert F.name in F.doc
    assert str(F) == F.doc
    assert set(F.extensions) == set(['.foo', '.bar', '.spam'])
    
    # Test setting extensions
    F1 = Format('test', '', 'foo bar spam')
    F2 = Format('test', '', 'foo, bar,spam')
    F3 = Format('test', '', ['foo', 'bar', 'spam'])
    F4 = Format('test', '', '.foo .bar .spam')
    for F in (F1, F2, F3, F4):
        assert set(F.extensions) == set(['.foo', '.bar', '.spam'])
    # Fail
    raises(ValueError, Format, 'test', '', 3)  # not valid ext
    raises(ValueError, Format, 'test', '', '', 3)  # not valid mode
    raises(ValueError, Format, 'test', '', '', 'x')  # not valid mode
    
    # Test subclassing
    F = MyFormat('test', '', modes='i')
    assert 'TEST DOCS' in F.doc
    
    # Get and check reader and write classes
    R = F.get_reader(Request(filename1, 'ri'))
    W = F.get_writer(Request(filename2, 'wi'))
    assert isinstance(R, MyFormat.Reader)
    assert isinstance(W, MyFormat.Writer)
    assert R.format is F
    assert W.format is F
    assert R.request.filename == filename1
    assert W.request.filename == filename2
    # Fail
    raises(RuntimeError, F.get_reader, Request(filename1, 'rI'))
    raises(RuntimeError, F.get_writer, Request(filename2, 'wI'))
    
    # Use as context manager
    with R:
        pass
    with W:
        pass
    # Objects are now closed, cannot be used
    assert R.closed
    assert W.closed
    #
    raises(RuntimeError, R.__enter__)
    raises(RuntimeError, W.__enter__)
    #
    raises(RuntimeError, R.get_data, 0)
    raises(RuntimeError, W.append_data, np.zeros((10, 10)))
    
    # Test __del__
    R = F.get_reader(Request(filename1, 'ri'))
    W = F.get_writer(Request(filename2, 'wi'))
    ids = id(R), id(W)
    F._closed[:] = []
    del R
    del W
    gc.collect()  # Invoke __del__
    assert set(ids) == set(F._closed)
Exemplo n.º 7
0
def test_format():
    """ Test the working of the Format class """

    filename1 = get_remote_file("images/chelsea.png", test_dir)
    filename2 = filename1 + ".out"

    # Test basic format creation
    F = Format("testname", "test description", "foo bar spam")
    assert F.name == "TESTNAME"
    assert F.description == "test description"
    assert F.name in repr(F)
    assert F.name in F.doc
    assert str(F) == F.doc
    assert set(F.extensions) == {".foo", ".bar", ".spam"}

    # Test setting extensions
    F1 = Format("test", "", "foo bar spam")
    F2 = Format("test", "", "foo, bar,spam")
    F3 = Format("test", "", ["foo", "bar", "spam"])
    F4 = Format("test", "", ".foo .bar .spam")
    for F in (F1, F2, F3, F4):
        assert set(F.extensions) == {".foo", ".bar", ".spam"}
    # Fail
    raises(ValueError, Format, "test", "", 3)  # not valid ext
    raises(ValueError, Format, "test", "", "", 3)  # not valid mode
    raises(ValueError, Format, "test", "", "", "x")  # not valid mode

    # Test subclassing
    F = MyFormat("test", "", modes="i")
    assert "TEST DOCS" in F.doc

    # Get and check reader and write classes
    R = F.get_reader(Request(filename1, "ri"))
    W = F.get_writer(Request(filename2, "wi"))
    assert isinstance(R, MyFormat.Reader)
    assert isinstance(W, MyFormat.Writer)
    assert R.format is F
    assert W.format is F
    assert R.request.filename == filename1
    assert W.request.filename == filename2
    # Fail
    raises(RuntimeError, F.get_reader, Request(filename1, "rI"))
    raises(RuntimeError, F.get_writer, Request(filename2, "wI"))

    # Use as context manager
    with R:
        pass
    with W:
        pass
    # Objects are now closed, cannot be used
    assert R.closed
    assert W.closed
    #
    raises(RuntimeError, R.__enter__)
    raises(RuntimeError, W.__enter__)
    #
    raises(RuntimeError, R.get_data, 0)
    raises(RuntimeError, W.append_data, np.zeros((10, 10)))

    # Test __del__
    R = F.get_reader(Request(filename1, "ri"))
    W = F.get_writer(Request(filename2, "wi"))
    ids = id(R), id(W)
    F._closed[:] = []
    del R
    del W
    gc.collect()  # Invoke __del__
    assert set(ids) == set(F._closed)
Exemplo n.º 8
0
 def __init__(self, *args, **kwargs):
     self._avbin = None
     Format.__init__(self, *args, **kwargs)