示例#1
0
def test_table_array_convert():
    """
    Test that structured arrays are converted when necessary, and
    reused as views when not.
    """
    from jwst.datamodels import util

    table_schema = {
        "allOf": [
            mschema.load_schema(os.path.join(os.path.dirname(__file__),
                                             "../schemas/image.schema.yaml"),
                                resolve_references=True), {
                                    "type": "object",
                                    "properties": {
                                        "table": {
                                            'title':
                                            'A structured table',
                                            'fits_hdu':
                                            'table',
                                            'datatype': [
                                                'bool8', {
                                                    'datatype': 'int16',
                                                    'name': 'my_int'
                                                }, {
                                                    'datatype': ['ascii', 64],
                                                    'name': 'my_string'
                                                }
                                            ]
                                        }
                                    }
                                }
        ]
    }

    table = np.array([(42, 32000, 'foo')],
                     dtype=[(str('f0'), str('?')), (str('my_int'), str('=i2')),
                            (str('my_string'), str('S64'))])

    x = util.gentle_asarray(table,
                            dtype=[(str('f0'), str('?')),
                                   (str('my_int'), str('=i2')),
                                   (str('my_string'), str('S64'))])

    assert x is table

    with DataModel(schema=table_schema) as x:
        x.table = table
        assert x.table is table

    table = np.array([(42, 32000, 'foo')],
                     dtype=[(str('f0'), str('?')), (str('my_int'), str('=i2')),
                            (str('my_string'), str('S3'))])

    with DataModel(schema=table_schema) as x:
        x.table = table
        assert x.table is not table
        assert x.table['my_string'][0] == table['my_string'][0]
示例#2
0
文件: test_schema.py 项目: sosey/jwst
def test_table_array_convert():
    """
    Test that structured arrays are converted when necessary, and
    reused as views when not.
    """
    from jwst.datamodels import util

    table_schema = {
        "allOf": [
            mschema.load_schema(
                os.path.join(os.path.dirname(__file__),
                             "../schemas/image.schema.yaml"),
                resolve_references=True),
            {
                "type": "object",
                "properties": {
                    "table": {
                        'title': 'A structured table',
                        'fits_hdu': 'table',
                        'datatype': [
                            'bool8',
                            {'datatype': 'int16',
                             'name': 'my_int'},
                            {'datatype': ['ascii', 64],
                             'name': 'my_string'}
                        ]
                    }
                }
            }
        ]
    }

    table = np.array(
        [(42, 32000, 'foo')],
        dtype=[
            (str('f0'), str('?')),
            (str('my_int'), str('=i2')),
            (str('my_string'), str('S64'))
            ])

    x = util.gentle_asarray(table, dtype=[
        (str('f0'), str('?')),
        (str('my_int'), str('=i2')),
        (str('my_string'), str('S64'))
    ])

    assert x is table

    with DataModel(schema=table_schema) as x:
        x.table = table
        assert x.table is table

    table = np.array(
        [(42, 32000, 'foo')],
        dtype=[
            (str('f0'), str('?')),
            (str('my_int'), str('=i2')),
            (str('my_string'), str('S3'))
            ])

    with DataModel(schema=table_schema) as x:
        x.table = table
        assert x.table is not table
        assert x.table['my_string'][0] == table['my_string'][0]