示例#1
0
def get_header_from_signal(signal, endianess="<"):
    header = get_default_header(endianess)
    if "blockfile_header" in signal.original_metadata:
        header = dict2sarray(signal.original_metadata["blockfile_header"],
                             sarray=header)
        note = signal.original_metadata["blockfile_header"]["Note"]
    else:
        note = ""
    # The navigation and signal units are 'nm' and 'cm', respectively, so we
    # convert the units accordingly before saving the signal
    axes_manager = signal.axes_manager.deepcopy()
    try:
        axes_manager.convert_units("navigation", "nm")
        axes_manager.convert_units("signal", "cm")
    except Exception:
        warnings.warn(
            "BLO file expects cm units in signal dimensions and nm in navigation dimensions. "
            "Existing units could not be converted; saving "
            "axes scales as is. Beware that scales "
            "will likely be incorrect in the file.",
            UserWarning,
        )

    if axes_manager.navigation_dimension == 2:
        NX, NY = axes_manager.navigation_shape
        SX = axes_manager.navigation_axes[0].scale
        SY = axes_manager.navigation_axes[1].scale
    elif axes_manager.navigation_dimension == 1:
        NX = axes_manager.navigation_shape[0]
        NY = 1
        SX = axes_manager.navigation_axes[0].scale
        SY = SX
    elif axes_manager.navigation_dimension == 0:
        NX = NY = SX = SY = 1

    DP_SZ = axes_manager.signal_shape
    if DP_SZ[0] != DP_SZ[1]:
        raise ValueError("Blockfiles require signal shape to be square!")
    DP_SZ = DP_SZ[0]
    SDP = 100.0 / axes_manager.signal_axes[0].scale

    offset2 = NX * NY + header["Data_offset_1"]
    # Based on inspected files, the DPs are stored at 16-bit boundary...
    # Normally, you'd expect word alignment (32-bits) ¯\_(°_o)_/¯
    offset2 += offset2 % 16

    header = dict2sarray(
        {
            "NX": NX,
            "NY": NY,
            "DP_SZ": DP_SZ,
            "SX": SX,
            "SY": SY,
            "SDP": SDP,
            "Data_offset_2": offset2,
        },
        sarray=header,
    )
    return header, note
示例#2
0
def get_header_from_signal(signal, endianess='<'):
    header = get_default_header(endianess)
    if 'blockfile_header' in signal.original_metadata:
        header = dict2sarray(signal.original_metadata['blockfile_header'],
                             sarray=header)
        note = signal.original_metadata['blockfile_header']['Note']
    else:
        note = ''
    # The navigation and signal units are 'nm' and 'cm', respectively, so we
    # convert the units accordingly before saving the signal
    axes_manager = signal.axes_manager.deepcopy()
    axes_manager.convert_units('navigation', 'nm')
    axes_manager.convert_units('signal', 'cm')

    if axes_manager.navigation_dimension == 2:
        NX, NY = axes_manager.navigation_shape
        SX = axes_manager.navigation_axes[0].scale
        SY = axes_manager.navigation_axes[1].scale
    elif axes_manager.navigation_dimension == 1:
        NX = axes_manager.navigation_shape[0]
        NY = 1
        SX = axes_manager.navigation_axes[0].scale
        SY = SX
    elif axes_manager.navigation_dimension == 0:
        NX = NY = SX = SY = 1

    DP_SZ = axes_manager.signal_shape
    if DP_SZ[0] != DP_SZ[1]:
        raise ValueError('Blockfiles require signal shape to be square!')
    DP_SZ = DP_SZ[0]
    SDP = 100. / axes_manager.signal_axes[0].scale

    offset2 = NX * NY + header['Data_offset_1']
    # Based on inspected files, the DPs are stored at 16-bit boundary...
    # Normally, you'd expect word alignment (32-bits) ¯\_(°_o)_/¯
    offset2 += offset2 % 16

    header = dict2sarray(
        {
            'NX': NX,
            'NY': NY,
            'DP_SZ': DP_SZ,
            'SX': SX,
            'SY': SY,
            'SDP': SDP,
            'Data_offset_2': offset2,
        },
        sarray=header)
    return header, note
示例#3
0
def test_d2s_arrayX():
    dt2 = dt + [('z', (np.uint8, 4)), ('u', (np.uint16, 4))]
    d = dict(z=2, u=[1, 2, 3, 4])
    sa = np.zeros((4,), dtype=dt2)
    sa = dict2sarray(d, sarray=sa)
    np.testing.assert_array_equal(sa['z'], [[2, 2, 2, 2], ] * 4)
    np.testing.assert_array_equal(sa['u'], [[1, 2, 3, 4], ] * 4)
示例#4
0
def test_d2s_arrayX():
    dt2 = dt + [('z', (np.uint8, 4)), ('u', (np.uint16, 4))]
    d = dict(z=2, u=[1, 2, 3, 4])
    sa = np.zeros((4,), dtype=dt2)
    sa = dict2sarray(d, sarray=sa)
    np.testing.assert_array_equal(sa['z'], [[2, 2, 2, 2], ] * 4)
    np.testing.assert_array_equal(sa['u'], [[1, 2, 3, 4], ] * 4)
示例#5
0
def test_d2s_arrayX():
    dt2 = dt + [("z", (np.uint8, 4)), ("u", (np.uint16, 4))]
    d = dict(z=2, u=[1, 2, 3, 4])
    sa = np.zeros((4,), dtype=dt2)
    sa = dict2sarray(d, sarray=sa)
    np.testing.assert_array_equal(sa["z"], [[2, 2, 2, 2]] * 4)
    np.testing.assert_array_equal(sa["u"], [[1, 2, 3, 4]] * 4)
示例#6
0
def test_d2s_extra_dict_ok():
    d = dict(x=5, y=10, text='abcdef', other=55)
    ref = np.zeros((1,), dtype=dt)
    ref['x'] = 5
    ref['y'] = 10
    ref['text'] = 'abcdef'

    nt.assert_equal(ref, dict2sarray(d, dtype=dt))
示例#7
0
def test_d2s_dtype():
    d = dict(x=5, y=10, text='abcdef')
    ref = np.zeros((1, ), dtype=dt)
    ref['x'] = 5
    ref['y'] = 10
    ref['text'] = 'abcdef'

    assert ref == dict2sarray(d, dtype=dt)
示例#8
0
def test_d2s_extra_dict_ok():
    d = dict(x=5, y=10, text="abcdef", other=55)
    ref = np.zeros((1,), dtype=dt)
    ref["x"] = 5
    ref["y"] = 10
    ref["text"] = "abcdef"

    nt.assert_equal(ref, dict2sarray(d, dtype=dt))
示例#9
0
def test_d2s_extra_dict_ok():
    d = dict(x=5, y=10, text='abcdef', other=55)
    ref = np.zeros((1, ), dtype=dt)
    ref['x'] = 5
    ref['y'] = 10
    ref['text'] = 'abcdef'

    assert ref == dict2sarray(d, dtype=dt)
示例#10
0
def test_d2s_dtype():
    d = dict(x=5, y=10, text='abcdef')
    ref = np.zeros((1,), dtype=dt)
    ref['x'] = 5
    ref['y'] = 10
    ref['text'] = 'abcdef'

    assert ref == dict2sarray(d, dtype=dt)
示例#11
0
def get_header_from_signal(signal, endianess='<'):
    header = get_default_header(endianess)
    if 'blockfile_header' in signal.original_metadata:
        header = dict2sarray(signal.original_metadata['blockfile_header'],
                             sarray=header)
        note = signal.original_metadata['blockfile_header']['Note']
    else:
        note = ''
    # The navigation and signal units are 'nm' and 'cm', respectively, so we
    # convert the units accordingly before saving the signal
    axes_manager = signal.axes_manager.deepcopy()
    axes_manager.convert_units('navigation', 'nm')
    axes_manager.convert_units('signal', 'cm')

    if axes_manager.navigation_dimension == 2:
        NX, NY = axes_manager.navigation_shape
        SX = axes_manager.navigation_axes[0].scale
        SY = axes_manager.navigation_axes[1].scale
    elif axes_manager.navigation_dimension == 1:
        NX = axes_manager.navigation_shape[0]
        NY = 1
        SX = axes_manager.navigation_axes[0].scale
        SY = SX
    elif axes_manager.navigation_dimension == 0:
        NX = NY = SX = SY = 1

    DP_SZ = axes_manager.signal_shape
    if DP_SZ[0] != DP_SZ[1]:
        raise ValueError('Blockfiles require signal shape to be square!')
    DP_SZ = DP_SZ[0]
    SDP = 100. / axes_manager.signal_axes[0].scale

    offset2 = NX * NY + header['Data_offset_1']
    # Based on inspected files, the DPs are stored at 16-bit boundary...
    # Normally, you'd expect word alignment (32-bits) ¯\_(°_o)_/¯
    offset2 += offset2 % 16

    header = dict2sarray({
        'NX': NX, 'NY': NY,
        'DP_SZ': DP_SZ,
        'SX': SX, 'SY': SY,
        'SDP': SDP,
        'Data_offset_2': offset2,
    }, sarray=header)
    return header, note
示例#12
0
def test_d2s_type_cast_ok():
    d = dict(x="34", text=55)

    ref = np.zeros((1,), dtype=dt)
    ref["x"] = 34
    ref["y"] = 0
    ref["text"] = "55"

    nt.assert_equal(ref, dict2sarray(d, dtype=dt))
示例#13
0
def test_d2s_type_cast_ok():
    d = dict(x='34', text=55)

    ref = np.zeros((1, ), dtype=dt)
    ref['x'] = 34
    ref['y'] = 0
    ref['text'] = '55'

    assert ref == dict2sarray(d, dtype=dt)
示例#14
0
def test_d2s_type_cast_ok():
    d = dict(x='34', text=55)

    ref = np.zeros((1,), dtype=dt)
    ref['x'] = 34
    ref['y'] = 0
    ref['text'] = '55'

    nt.assert_equal(ref, dict2sarray(d, dtype=dt))
示例#15
0
def test_d2s_sarray():
    d = dict(x=5, y=10, text='abcdef')

    base = np.zeros((1, ), dtype=dt)
    base['x'] = 65
    base['text'] = 'gg'

    ref = np.zeros((1, ), dtype=dt)
    ref['x'] = 5
    ref['y'] = 10
    ref['text'] = 'abcdef'

    assert ref == dict2sarray(d, sarray=base)
示例#16
0
def test_d2s_partial_sarray():
    d = dict(text='abcdef')

    base = np.zeros((1, ), dtype=dt)
    base['x'] = 65
    base['text'] = 'gg'

    ref = np.zeros((1, ), dtype=dt)
    ref['x'] = 65
    ref['y'] = 0
    ref['text'] = 'abcdef'

    assert ref == dict2sarray(d, sarray=base)
示例#17
0
def test_d2s_partial_sarray():
    d = dict(text="abcdef")

    base = np.zeros((1,), dtype=dt)
    base["x"] = 65
    base["text"] = "gg"

    ref = np.zeros((1,), dtype=dt)
    ref["x"] = 65
    ref["y"] = 0
    ref["text"] = "abcdef"

    nt.assert_equal(ref, dict2sarray(d, sarray=base))
示例#18
0
def test_d2s_sarray():
    d = dict(x=5, y=10, text='abcdef')

    base = np.zeros((1,), dtype=dt)
    base['x'] = 65
    base['text'] = 'gg'

    ref = np.zeros((1,), dtype=dt)
    ref['x'] = 5
    ref['y'] = 10
    ref['text'] = 'abcdef'

    assert ref == dict2sarray(d, sarray=base)
示例#19
0
def test_d2s_partial_sarray():
    d = dict(text='abcdef')

    base = np.zeros((1,), dtype=dt)
    base['x'] = 65
    base['text'] = 'gg'

    ref = np.zeros((1,), dtype=dt)
    ref['x'] = 65
    ref['y'] = 0
    ref['text'] = 'abcdef'

    nt.assert_equal(ref, dict2sarray(d, sarray=base))
示例#20
0
def test_d2s_fail():
    d = dict(x=5, y=10, text='abcdef')
    with pytest.raises(ValueError):
        dict2sarray(d)
示例#21
0
def test_d2s_array2():
    d = dict(x=2, y=[1, 2, 3, 4])
    sa = np.zeros((4, ), dtype=dt)
    sa = dict2sarray(d, sarray=sa)
    np.testing.assert_array_equal(sa['x'], [2, 2, 2, 2])
    np.testing.assert_array_equal(sa['y'], [1, 2, 3, 4])
示例#22
0
def test_d2s_array1():
    dt2 = dt + [('z', (np.uint8, 4)), ('u', (np.uint16, 4))]
    d = dict(z=2, u=[1, 2, 3, 4])
    sa = dict2sarray(d, dtype=dt2)
    np.testing.assert_array_equal(sa['z'][0], [2, 2, 2, 2])
    np.testing.assert_array_equal(sa['u'][0], [1, 2, 3, 4])
示例#23
0
def test_d2s_string_cut():
    d = dict(text="Testerstring")
    sa = dict2sarray(d, dtype=dt)
    nt.assert_equal(sa["text"][0], "Tester")
示例#24
0
def test_d2s_string_cut():
    d = dict(text='Testerstring')
    sa = dict2sarray(d, dtype=dt)
    nt.assert_equal(sa['text'][0], 'Tester')
示例#25
0
def test_d2s_array1():
    dt2 = dt + [('z', (np.uint8, 4)), ('u', (np.uint16, 4))]
    d = dict(z=2, u=[1, 2, 3, 4])
    sa = dict2sarray(d, dtype=dt2)
    np.testing.assert_array_equal(sa['z'][0], [2, 2, 2, 2])
    np.testing.assert_array_equal(sa['u'][0], [1, 2, 3, 4])
示例#26
0
def test_d2s_string_cut():
    d = dict(text='Testerstring')
    sa = dict2sarray(d, dtype=dt)
    assert sa['text'][0] == b'Tester'
示例#27
0
def test_d2s_type_cast_invalid():
    d = dict(x='Test')
    dict2sarray(d, dtype=dt)
示例#28
0
def test_d2s_type_cast_invalid():
    d = dict(x='Test')
    with pytest.raises(ValueError):
        dict2sarray(d, dtype=dt)
示例#29
0
def test_d2s_array2():
    d = dict(x=2, y=[1, 2, 3, 4])
    sa = np.zeros((4,), dtype=dt)
    sa = dict2sarray(d, sarray=sa)
    np.testing.assert_array_equal(sa['x'], [2, 2, 2, 2])
    np.testing.assert_array_equal(sa['y'], [1, 2, 3, 4])
示例#30
0
def test_d2s_string_cut():
    d = dict(text='Testerstring')
    sa = dict2sarray(d, dtype=dt)
    nt.assert_equal(sa['text'][0], 'Tester')
示例#31
0
def test_d2s_type_cast_invalid():
    d = dict(x='Test')
    dict2sarray(d, dtype=dt)
示例#32
0
def test_d2s_type_cast_invalid():
    d = dict(x='Test')
    with pytest.raises(ValueError):
        dict2sarray(d, dtype=dt)
示例#33
0
def test_d2s_fail():
    d = dict(x=5, y=10, text='abcdef')
    dict2sarray(d)
示例#34
0
def test_d2s_fail():
    d = dict(x=5, y=10, text='abcdef')
    with pytest.raises(ValueError):
        dict2sarray(d)
示例#35
0
def test_d2s_string_cut():
    d = dict(text='Testerstring')
    sa = dict2sarray(d, dtype=dt)
    assert sa['text'][0] == b'Tester'
示例#36
0
def test_d2s_fail():
    d = dict(x=5, y=10, text='abcdef')
    dict2sarray(d)